import 'package:flutter/material.dart';

import '../../common/colors.dart';
import '../../services/address.dart';
import '../../services/dio_manager.dart';
import '../personnel_module/personnel_model.dart';
import 'contact_model.dart';
import 'index_bar.dart';
import 'package:get/get.dart';

class ContactOrganPage extends StatefulWidget {
  ContactOrganPage({Key? key, required this.positid}) : super(key: key) {}

  var positid = 0;

  @override
  _ContactOrganPageState createState() => _ContactOrganPageState();
}

class _ContactOrganPageState extends State<ContactOrganPage> {
  late List<Data>? _listDatas = []; //列表数据
  late ScrollController _scrollController; //滚动控制器

  final double _cellHeight = 54.0;
  final double _groupHeight = 30.0;

  //页面创建渲染的时候走这个方法
  @override
  void initState() {
    super.initState();
    getInitData();
    //创建数据
    _scrollController = ScrollController(); //初始化
  }

  void getInitData() async {
    print("OrganPage==print>>");
    var json = await DioManager()
        .kkRequest(Address.hrEmployee, params: {'': ''}, method: 'get');
    PersonnelModel _model = PersonnelModel.fromJson(json);
    _listDatas = _model.data;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColor.bgColor,
      appBar: AppBar(
        actions: [],
        title: const Text(
          '选择负责人',
          style: TextStyle(color: Colors.black),
        ),
      ),
      body: Stack(
        children: [
          Container(
            margin: EdgeInsets.only(top: 15),
            child: ListView.builder(
              itemBuilder: _itemForRow,
              itemCount: _listDatas!.length,
              controller: _scrollController, //控制器
            ),
          ),
        ],
      ),
    );
  }

  Widget _itemForRow(BuildContext context, int index) {
    return _FriendCell(
        onPressed: () {
          print("index==>>>$index");
          setState(() {
            widget.positid = _listDatas![index].userId!;
            if (widget.positid > 0) {
              Get.back(result: {
                'data': 'refresh',
                'name': _listDatas![index].name,
                'position': _listDatas![index].userId!
              });
            }
          });
        },
        userid: _listDatas![index].userId,
        currentid: widget.positid,
        imageUrl: _listDatas![index].image128,
        name: _listDatas![index].name,
        groupTitle: null);
  }
}

class _FriendCell extends StatelessWidget {
  final int? userid; //用户ID
  final int? currentid; //位置
  // final Friends? friends; //员工item
  final String? imageUrl; //图片 URL
  final String? name; //昵称
  final String? groupTitle; //组头标题
  final String? imageAssets; //本地图片地址

  final VoidCallback? onPressed;

  const _FriendCell(
      {@required this.onPressed,
      this.userid,
      this.currentid,
      this.imageUrl,
      this.name,
      this.groupTitle,
      this.imageAssets});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        //组头
        Container(
          alignment: Alignment.centerLeft,
          //靠左边居中
          padding: const EdgeInsets.only(left: 10),
          //距离左10 个间距
          height: groupTitle != null ? 30 : 0,
          color: AppColor.bgColor,
          child: groupTitle != null
              ? Text(
                  groupTitle!,
                  style: const TextStyle(color: Colors.grey),
                )
              : null,
        ),
        //昵称和图片
        GestureDetector(
            onTap: onPressed,
            // onTap: () {
            // Get.back(result: {
            //   'data': 'refresh',
            //   'name': name!,
            //   'position': userid!
            // });
            // print("userid==>>>$userid");
            // print("current==>>>$current");
            // },
            child: Container(
              color: Colors.white,
              child: Row(children: [
                Container(
                    margin: const EdgeInsets.all(10),
                    width: 30,
                    height: 30,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(6.0),
                        image: DecorationImage(
                          image: getImage(),
                        ))), //图片
                // ignore: avoid_unnecessary_containers
                Container(
                    width: Get.width - 124,
                    child: Column(
                      children: [
                        Container(
                          alignment: Alignment.centerLeft,
                          height: 53.5,
                          child: Text(
                            name!,
                            style: TextStyle(
                                fontSize: 16,
                                color:
                                    userid! > 0 ? Colors.black : Colors.grey),
                          ),
                        ),
                        Container(
                          height: 0.5,
                          color: AppColor.lineColor,
                        ), //下划线
                      ],
                    )),
                Visibility(
                    visible: userid! > 0,
                    child: Container(
                      margin: const EdgeInsets.all(
                        12,
                      ),
                      child: Image.asset(
                        currentid == userid
                            ? 'images/check_image.png'
                            : 'images/uncheck_image.png',
                        width: 24,
                        height: 24,
                      ),
                    )), //昵称
              ]),
            )),
      ],
    );
  }

  ImageProvider getImage() {
    if (imageUrl == null) {
      return AssetImage(imageAssets!);
    }
    return NetworkImage(Address.homeHost + imageUrl!);
  }
}
